package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcServiceItemMapper;
import com.csun.cmny.provider.dto.ReportItemDto;
import com.csun.cmny.provider.dto.ServiceItemDto;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcReportItem;
import com.csun.cmny.provider.model.domain.OmcServiceItem;
import com.csun.cmny.provider.service.OmcReportItemService;
import com.csun.cmny.provider.service.OmcServiceItemService;
import com.csun.cmny.provider.vo.ServiceItemVo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Transactional
public class OmcServiceItemServiceImpl extends BaseService<OmcServiceItem> implements OmcServiceItemService {

    @Resource
    OmcServiceItemMapper omcServiceItemMapper;

    @Resource
    OmcReportItemService omcReportItemService;

    @Override
    public List<ServiceItemVo> selectByServiceId(Long serviceId) {

        return omcServiceItemMapper.selectByServiceId(serviceId);
    }

    @Override
    public Integer saveServiceItem(List<ServiceItemDto> serviceItemDtos, LoginAuthDto loginAuthDto) {
//        for(OmcServiceItem omcServiceItem : omcServiceItems) {
//                int addServiceItemRet = omcServiceItemMapper.insertSelective(omcServiceItem);
//                List<OmcReportItem> omcReportItems = new ArrayList<>();
//                for (ReportItemDto item : omcServiceItem.getReportItemDtos()) {
//                    item.setServiceItemId(omcServiceItem.getId());
//                    OmcReportItem omcReportItem = new OmcReportItem();
//                    BeanUtils.copyProperties(item, omcReportItem);
//                    omcReportItems.add(omcReportItem);
//                }
//                int addReportItemRet = omcReportItemService.batchSave(omcReportItems);
//                if (addReportItemRet < 0 && addServiceItemRet < 0) {
//                    throw new OmcBizException(ErrorCodeEnum.OMC10033002);
//                }
//            }
        List<OmcServiceItem> omcServiceItems = Lists.newArrayList();
        List<OmcReportItem> omcReportItems = new ArrayList<>();
        //copy服务项属性List
        for (ServiceItemDto serviceItemDto : serviceItemDtos) {
            OmcServiceItem omcServiceItem = new OmcServiceItem();
            BeanUtils.copyProperties(serviceItemDto, omcServiceItem);
            omcServiceItems.add(omcServiceItem);
        }
        //插入单个服务项时，数据库中校验是否同名
        if(omcServiceItems.size()<=1){
            if(!checkName(omcServiceItems.get(0))){
                throw new OmcBizException(ErrorCodeEnum.OMC10033003);
            }
        //插入服务项list时，去除同名服务项
        }else{
            //去除list中itemName同名元素
            for (int i = 0; i < omcServiceItems.size()-1; i++) {
                for (int j = omcServiceItems.size()-1; j > i; j--) {
                    if (omcServiceItems.get(j).getItemName().equals(omcServiceItems.get(i).getItemName())) {
                        omcServiceItems.remove(j);
                    }
                }
            }
        }
        //批量插入服务项
        int addServiceItemRet = batchSave(omcServiceItems);
        //copy异常报告List
        for (OmcServiceItem omcServiceItem : omcServiceItems) {
            if(omcServiceItem.getReportItemDtos() != null){
                List<ReportItemDto> reportItemDtos =  omcServiceItem.getReportItemDtos();
                for (ReportItemDto item : reportItemDtos) {
                    item.setServiceItemId(omcServiceItem.getId());
                    OmcReportItem omcReportItem = new OmcReportItem();
                    BeanUtils.copyProperties(item, omcReportItem);
                    omcReportItems.add(omcReportItem);
                }
            }
        }
        //批量添加异常报告
        int addReportItemRet = omcReportItemService.batchSave(omcReportItems);
        if (addServiceItemRet >= 0 && addReportItemRet >= 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public Integer deleteServiceItemAndReportItem(Long serviceItemId) {
        int deleteServiceItemRet = omcServiceItemMapper.deleteByPrimaryKey(serviceItemId);
        OmcReportItem omcReportItem = new OmcReportItem();
        omcReportItem.setServiceItemId(serviceItemId);
        int deleteReportItemRet = omcReportItemService.delete(omcReportItem);
        if(deleteServiceItemRet >=0 && deleteReportItemRet >=0){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public Integer updateByPrimarykey(OmcServiceItem omcServiceItem) {
       /* OmcServiceItem omcServiceItemQuery = new OmcServiceItem();
        omcServiceItemQuery.setItemName(omcServiceItem.getItemName());
        omcServiceItemQuery.setServiceId(omcServiceItem.getServiceId());
        List<OmcServiceItem> omcServiceItems = omcServiceItemMapper.select(omcServiceItemQuery);
       */
        if(!checkName(omcServiceItem)){
            throw new OmcBizException(ErrorCodeEnum.OMC10033003);
        }else{
            return omcServiceItemMapper.updateByPrimaryKeySelective(omcServiceItem);
        }
    }

    /*
    * 校验名称是否相同
    * */
    public boolean checkName(OmcServiceItem omcServiceItem){
        Example example = new Example(OmcServiceItem.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemName", omcServiceItem.getItemName());
        criteria.andEqualTo("serviceId", omcServiceItem.getServiceId());
        if(omcServiceItem.getId() != null){
            criteria.andNotEqualTo("id", omcServiceItem.getId());
        }
        int result = selectCountByExample(example);
        return result<1;
    }
}
